package com.ang.util;

import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @author ssang
 * @date 2023-04-21 11:29
 */
public class WordUtils {

    /**
     * 渲染Word内容
     * @param data              数据
     * @param templateFilePath  模板文件path
     * @param targetFilePath    目标文件path
     */
    public static boolean renderWordWithTemplate(JSONObject data, String templateFilePath, String targetFilePath){
        XWPFTemplate template = null;
        XWPFDocument doc  = null;
        FileOutputStream outputStream = null;
        try{
            //渲染文档
            template = XWPFTemplate.compile(templateFilePath).render(data);

            //获取文档
            doc = template.getXWPFDocument();

            outputStream = new FileOutputStream(targetFilePath);

            doc.write(outputStream);

            return true;
        }catch (Exception e){
            return false;
        }
        finally {

        }
    }
}
